﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MazeMaker
{
    class MazeTree
    {
        private int[] Tree;
        public int Size  { private set; get; }

        public MazeTree()
        {
            Tree = new int[600];
            Size = 600;
            for (int i = 0; i < 600; i++ )
            {
                Tree[i] = -1;
            }
        }
                      
        public int Find(int i)
        {
            if (Tree[i] < 0)
            {                
                return i;
            }
            else
            {
                int Root = Find(Tree[i]);
                Tree[i] = Root;
                return Root;
            }
        }

        public int GetValue(int i)
        {
            return Tree[i];
        }

                
        public void Union(int x, int y)
        {
            int root1 = Find(x);
            int root2 = Find(y);
            Size--;

            if(Tree[root1] < Tree[root2])
            {
                int temp = Tree[root2];
                Tree[root2] = root1;                                                    
                Tree[root1] += temp;            
            }
            else if (Tree[root1] >= Tree[root2])
            {
                int temp = Tree[root1];
                Tree[root1] = root2;
                Tree[root2] += temp;
            }
        }
    }
}
